import { api } from '@/utils/request'
import type { 
  PostTag, 
  PostTagPageParams, 
  PostTagRequest, 
  PageResult,
  ApiResponse,
  TagOverview,
  HotTagRank
} from '@/types'

/**
 * 话题管理API服务
 */
export class TagService {
  /**
   * 分页查询话题列表
   */
  static async getTagPage(params: PostTagPageParams): Promise<PageResult<PostTag>> {
    const response = await api.post<PageResult<PostTag>>('/circle/api-admin/sPostTag/page', params)
    return response.data
  }

  /**
   * 获取话题详情
   */
  static async getTagDetail(id: number): Promise<PostTag> {
    const response = await api.get<PostTag>(`/circle/api-admin/sPostTag/get/${id}`)
    return response.data
  }

  /**
   * 新增话题
   */
  static async createTag(tagData: PostTagRequest): Promise<boolean> {
    const response = await api.post<boolean>('/circle/api-admin/sPostTag/create', tagData)
    return response.data
  }

  /**
   * 更新话题
   */
  static async updateTag(tagData: PostTagRequest): Promise<boolean> {
    const response = await api.post<boolean>('/circle/api-admin/sPostTag/update', tagData)
    return response.data
  }

  /**
   * 删除话题
   */
  static async deleteTag(id: number): Promise<boolean> {
    const response = await api.delete<boolean>(`/circle/api-admin/sPostTag/delete/${id}`)
    return response.data
  }

  /**
   * 批量删除话题
   */
  static async batchDeleteTags(ids: number[]): Promise<boolean> {
    const deletePromises = ids.map(id => this.deleteTag(id))
    await Promise.all(deletePromises)
    return true
  }

  /**
   * 置顶话题
   */
  static async pinTag(id: number, tagSort: number = 1): Promise<boolean> {
    return this.updateTag({
      id,
      isPinned: 1,
      tagSort,
    })
  }

  /**
   * 取消置顶话题
   */
  static async unpinTag(id: number): Promise<boolean> {
    return this.updateTag({
      id,
      isPinned: 0,
    })
  }

  /**
   * 批量置顶话题
   */
  static async batchPinTags(ids: number[]): Promise<boolean> {
    const pinPromises = ids.map(id => this.pinTag(id))
    await Promise.all(pinPromises)
    return true
  }

  /**
   * 批量取消置顶话题
   */
  static async batchUnpinTags(ids: number[]): Promise<boolean> {
    const unpinPromises = ids.map(id => this.unpinTag(id))
    await Promise.all(unpinPromises)
    return true
  }

  /**
   * 获取话题概览
   * GET /circle/api-admin/sPostTag/overview
   */
  static async getTagOverview(): Promise<TagOverview> {
    const response = await api.get<TagOverview>('/circle/api-admin/sPostTag/overview')
    return response.data
  }

  /**
   * 获取热门话题排行
   * GET /circle/api-admin/sPostTag/hotRank
   */
  static async getHotTagRank(): Promise<HotTagRank[]> {
    const response = await api.get<HotTagRank[]>('/circle/api-admin/sPostTag/hotRank')
    return response.data
  }
}

// 默认导出单例实例
export const tagService = TagService 